<?php
Globals::requireClass('Controller');
Globals::requireModel('User');
Globals::requireTable('Recommendseetype');
Globals::requireTable('Recommendsee');
Globals::requireTable('Recommendseetouid');
Globals::requireTable('Recommendseedetail');

class RecommendseeController extends Controller
{
    protected $Recommendseetype;
    protected $Recommendsee;
    protected $Recommendseetouid;
    protected $Recommendseedetail;
	
	public static $defaultConfig = array(
		'viewEnabled'	=> true,
		'layoutEnabled'	=> true,
		'title'			=> null
	);
	
	public function __construct($config = null)
	{
		parent::__construct($config);
		$this->Recommendseetype = new RecommendseetypeTable($config);
		$this->Recommendsee = new RecommendseeTable($config);
		$this->Recommendseetouid = new RecommendseetouidTable($config);
		$this->Recommendseedetail = new RecommendseedetailTable($config);
		dolog(json_encode($_GET));
	}
	
	public function indexAction()
	{
		$this->layout->pageTitle	= "test";
	}
	
	
	public function recommendseetypeAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','list'=>array()));
	        exit();
	    }
	    
	    $list = array();
	    
	    $data = $this->Recommendseetype->listAll(array(),'id asc');
        foreach ($data as $v){
            $tmp = array();
            $tmp['title'] = $v['title'];
            $tmp['type'] = $v['id'];
            $list[] = $tmp;
        }
        
	    echo json_encode(array('returncode'=>'0','list'=>$list));
	    exit();
	}
	
	public function recommendseelistAction(){
	    $uid = $this->getParam('uid');
	    $token = $this->getParam('token');
	    $machine_type = $this->getParam('machine_type');
	    $usermodel = new UserModel($this->config);
	    $uid = $usermodel->getUserBase($uid, $token ,$machine_type);
	    if($uid < 1){
	        echo json_encode(array('returncode'=>'-1','list'=>array()));
	        exit();
	    }
	    
	    $type = $this->getIntParam('type');
	    $page = $this->getIntParam('page');
	    $page = $page < 1 ? 1 : $page;
	    $size = $this->getIntParam('size');
	    $size = $size < 1 ? 10 : $size;
	    
	    $list = array();
	    
	    $where = array('type'=>$type);
	    $data = $this->Recommendsee->listPage($where, 'id DESC', $size, $page);
	    foreach ($data as $v){
	        $isr = $this->Recommendseetouid->getRow(array('rid'=>$v['id'],'uid'=>$uid));
	        
	        $tmp_detail = array();
	        $detail_data = $this->Recommendseedetail->listAll(array('rid'=>$v['id']));
	        foreach ($detail_data as $v2){
	            $tmpd = array(
	                'icon' => $v2['icon'],
	                'name' => $v2['name'],
	                'describe' => $v2['remark'],
	                'playtypename' => $v2['playtypename'],
	                'playurl' => $v2['playurl']
	            );
	            $tmp_detail[] = $tmpd;
	        }
	        
	        $tmp = array(
	            'title' => $v['title'],
	            'icon' => $v['icon'],
	            'describe' => $v['remark'],
	            'collect' => (isset($isr['id']) && $isr['id'] > 0) ? 1 : 0,
	            'id' => $v['id'],
	            'playterrace' => $tmp_detail
	        );
	        
	        $list[] = $tmp;
	    }
	    
	    echo json_encode(array('returncode'=>'0','list'=>$list));
	    exit();
	}
	
	protected function out()
	{
		$this->layout->nav		= 'index';
		parent::out();
	}
}

Config::extend('RecommendseeController', 'Controller');
